package com.cn.minio.controller;

import com.cn.minio.config.MinioClientConfig;
import com.cn.minio.utils.MinioUtil;
import com.cn.minio.utils.ResponseInfo;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Description :
 * @Author : Bruce Lee
 * @CreateTime : 2024/6/21
 */

@Slf4j
@RestController
@RequestMapping("/minio")
public class MinIoController {

    @Autowired
    private MinioUtil minioUtil;

    @Value("${s3.bucketName}")
    private String bucketName;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public ResponseInfo uploadFile(@RequestBody MultipartFile file, String fileName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (Objects.isNull(minioClient)) {
            return ResponseInfo.error("连接MinIO服务器失败");
        }
        Integer result = minioUtil.minioUpload(file, fileName, bucketName, minioClient);
        if (result == 200) {
            return ResponseInfo.success("文件上传成功");
        } else {
            return ResponseInfo.error("文件上传错误！！！");
        }
    }

    /**
     * 获取文件预览地址
     *
     * @param fileName 文件名 + 后缀
     */
    @PostMapping("/getRedFile")
    public ResponseInfo getRedFile(String fileName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (Objects.isNull(minioClient)) {
            return ResponseInfo.error("连接MinIO服务器失败");
        }
        String url = minioUtil.getPreviewFileUrl(bucketName, fileName, minioClient);
        if (url == null) {
            return ResponseInfo.error("文件预览地址获取失败");
        }
        return ResponseInfo.success(url);
    }

    /**
     * 下载文件
     *
     * @param fileName 文件名 + 后缀
     * @param response
     * @return
     */
    @RequestMapping("/downloadFile")
    public String downloadFile(@RequestParam String fileName, HttpServletResponse response) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (Objects.isNull(minioClient)) {
            return "连接MinIO服务器失败";
        }
        return minioUtil.downloadFile(bucketName, fileName, response) != null ? "下载成功" : "下载失败";
    }

    /**
     * 删除单个文件
     */
    @PostMapping("/deleteFile")
    public String deleteFile(String fileName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return "连接MinIO服务器失败";
        }
        return minioUtil.deleteBucketFile(bucketName, fileName, minioClient) ? "删除成功" : "删除失败";
    }

    /**
     * 删除桶下面所有文件
     */
    @PostMapping("/deleteAllFile")
    public String deleteAllFile(String bucketName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return "连接MinIO服务器失败";
        }
        minioUtil.deleteBucketAllFile(bucketName, minioClient);
        return "删除成功";
    }

    /**
     * 创建桶
     */
    @PostMapping("/createBucket")
    public String createBucket(String bucketName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return "连接MinIO服务器失败";
        }
        Integer result = minioUtil.createBucketName(bucketName);
        if (result == 200) {
            return "创建成功";
        } else {
            return "创建失败";
        }
    }

    /**
     * 删除桶
     */
    @PostMapping("/deleteBucket")
    public String deleteBucket(String bucketName) {
        MinioClient minioClient = MinioClientConfig.getMinioClient();
        if (minioClient == null) {
            return "连接MinIO服务器失败";
        }
        Integer result = minioUtil.deleteBucket(bucketName);
        if (result == 200) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}
